/* SQUIB - Scala's Quirky User Interface Builder 
    Copyright (C) 2008 Tim Dalton

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

import scala.collection.mutable.{HashMap}
    
package tfd.scala.squib.scenario.scenegraph {

import java.awt.Component
import java.awt.event.{MouseEvent, MouseWheelEvent}
import java.util.EventObject
import com.sun.scenario.scenegraph._
import com.sun.scenario.scenegraph.event._

trait FiresMouseEventsWithNode[T <: {  def addMouseListener(ml:SGMouseListener) }] { self:FiresEventObjectsWithNode with IdableTrait => 
    self.eventHandlers += (mouseEvents _).asInstanceOf[(Object, HashMap[String, (EventObject, SGNode) => Unit]) => Unit]

	def mouseEvents(comp: T, events:HashMap[String, (EventObject, SGNode) => Unit]):T = {
        val handler = new DelegateMouseEventWithNodeHandler(events);
        if (handler.hasEvents) {
            comp.addMouseListener(handler)
        }
        comp
    }
    
    class DelegateMouseEventWithNodeHandler(events: HashMap[String, (EventObject, SGNode) => Unit]) extends SGMouseListener {
        private val mouseEnteredDelegate:Option[(MouseEvent, SGNode) => Unit] = events.get("mouseentered");
        private val mouseExitedDelegate:Option[(MouseEvent, SGNode) => Unit] = events.get("mouseexited");
        private val mouseClickedDelegate:Option[(MouseEvent, SGNode) => Unit] = events.get("mouseclicked");
        private val mousePressedDelegate:Option[(MouseEvent, SGNode) => Unit] = events.get("mousepressed");
        private val mouseReleasedDelegate:Option[(MouseEvent, SGNode) => Unit] = events.get("mousereleased");
        private val mouseDraggedDelegate:Option[(MouseEvent, SGNode) => Unit] = events.get("mousedragged");
        private val mouseMovedDelegate:Option[(MouseEvent, SGNode) => Unit] = events.get("mousemoved");
        private val mouseWheelMovedDelegate:Option[(MouseWheelEvent, SGNode) => Unit] = events.get("mousewheelmoved");
        
        lazy val hasEvents = !(mouseEnteredDelegate.isEmpty 
                          && mouseExitedDelegate.isEmpty
                          && mouseClickedDelegate.isEmpty
                          && mousePressedDelegate.isEmpty
                          && mouseReleasedDelegate.isEmpty
                          && mouseDraggedDelegate.isEmpty
                          && mouseMovedDelegate.isEmpty
                          && mouseWheelMovedDelegate.isEmpty)
                
        override def mouseEntered(me: MouseEvent, node:SGNode) {
            if (!mouseEnteredDelegate.isEmpty) mouseEnteredDelegate.get.apply(me, node)
        }
        
        override def mouseExited(me: MouseEvent, node:SGNode) {
            if (!mouseExitedDelegate.isEmpty) mouseExitedDelegate.get.apply(me, node)
        }
        
        override def mouseClicked(me: MouseEvent, node:SGNode) {
            if (!mouseClickedDelegate.isEmpty) mouseClickedDelegate.get.apply(me, node)
        }
        
        override def mousePressed(me: MouseEvent, node:SGNode) {
            if (!mousePressedDelegate.isEmpty) mousePressedDelegate.get.apply(me, node)
        }
        
        override def mouseReleased(me: MouseEvent, node:SGNode) {
            if (!mouseReleasedDelegate.isEmpty) mouseReleasedDelegate.get.apply(me, node)
        }
        
        override def mouseDragged(me: MouseEvent, node:SGNode) {
            if (!mouseDraggedDelegate.isEmpty) mouseDraggedDelegate.get.apply(me, node)
        }
        
        override def mouseMoved(me: MouseEvent, node:SGNode) {
            if (!mouseMovedDelegate.isEmpty) mouseMovedDelegate.get.apply(me, node)
        }
        
        override def mouseWheelMoved(mwe: MouseWheelEvent, node:SGNode) {
            if (!mouseWheelMovedDelegate.isEmpty) mouseWheelMovedDelegate.get.apply(mwe, node)
        }
    }
}
}
